
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>代码生成 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-emphasize/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-codeblock-label/block.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-click-reveal/click_reveal.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="../../styles/website.css">
        
    
        
    
        
    
        
    
        
    
        
    


    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="part4-3-task.html" />
    
    
    <link rel="prev" href="part4-1-cconv.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    实验简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../log.html">
            
                <a href="../log.html">
            
                    
                    更新日志
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">零：前置准备</li>
        
        
    
        <li class="chapter " data-level="2.1" >
            
                <span>
            
                    
                    配环境、跑测试
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="../lab0/env.html">
            
                <a href="../lab0/env.html">
            
                    
                    环境配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../lab0/testing.html">
            
                <a href="../lab0/testing.html">
            
                    
                    运行测试样例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../lab0/riscv.html">
            
                <a href="../lab0/riscv.html">
            
                    
                    RISC-V 的工具链使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">第一个编译器</li>
        
        
    
        <li class="chapter " data-level="3.1" >
            
                <span>
            
                    
                    step1：仅一个 return 的 main 函数
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../lab1/part1.html">
            
                <a href="../lab1/part1.html">
            
                    
                    从零开始的 lexer、parser 以及汇编生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../lab1/part2.html">
            
                <a href="../lab1/part2.html">
            
                    
                    词法语法分析工具
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../lab1/part3.html">
            
                <a href="../lab1/part3.html">
            
                    
                    使用中间码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4" >
            
                <span>
            
                    
                    其他
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.4.1" data-path="../lab1/antlr.html">
            
                <a href="../lab1/antlr.html">
            
                    
                    ANTLR 使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4.2" data-path="../lab1/visitor.html">
            
                <a href="../lab1/visitor.html">
            
                    
                    Visitor 模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4.3" data-path="../lab1/ir.html">
            
                <a href="../lab1/ir.html">
            
                    
                    IR 简明介绍
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.1.5" data-path="../lab1/spec.html">
            
                <a href="../lab1/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">常量表达式</li>
        
        
    
        <li class="chapter " data-level="4.1" >
            
                <span>
            
                    
                    step2：一元操作
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="../lab2/intro.html">
            
                <a href="../lab2/intro.html">
            
                    
                    任务概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="../lab2/guide.html">
            
                <a href="../lab2/guide.html">
            
                    
                    实验指导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.3" data-path="../lab2/spec.html">
            
                <a href="../lab2/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.2" >
            
                <span>
            
                    
                    step3：加减乘除模
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.2.1" data-path="../lab3/intro.html">
            
                <a href="../lab3/intro.html">
            
                    
                    任务概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2.2" data-path="../lab3/guide.html">
            
                <a href="../lab3/guide.html">
            
                    
                    实验指导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2.3" data-path="../lab3/spec.html">
            
                <a href="../lab3/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.3" >
            
                <span>
            
                    
                    step4：比较和逻辑表达式
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.3.1" data-path="../lab4/intro.html">
            
                <a href="../lab4/intro.html">
            
                    
                    任务概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.2" data-path="../lab4/guide.html">
            
                <a href="../lab4/guide.html">
            
                    
                    实验指导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.3" data-path="../lab4/spec.html">
            
                <a href="../lab4/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">变量和语句</li>
        
        
    
        <li class="chapter " data-level="5.1" >
            
                <span>
            
                    
                    step5：局部变量和赋值
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1.1" data-path="../lab5/intro.html">
            
                <a href="../lab5/intro.html">
            
                    
                    任务概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.2" data-path="../lab5/guide.html">
            
                <a href="../lab5/guide.html">
            
                    
                    实验指导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.3" >
            
                <span>
            
                    
                    其他
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1.3.1" data-path="../lab5/stackframe.html">
            
                <a href="../lab5/stackframe.html">
            
                    
                    栈帧
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5.1.4" data-path="../lab5/spec.html">
            
                <a href="../lab5/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5.2" >
            
                <span>
            
                    
                    step6：if 语句和条件表达式
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.2.1" data-path="../lab6/intro.html">
            
                <a href="../lab6/intro.html">
            
                    
                    任务概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2.2" data-path="../lab6/guide.html">
            
                <a href="../lab6/guide.html">
            
                    
                    实验指导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2.3" data-path="../lab6/spec.html">
            
                <a href="../lab6/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">块语句和作用域和更多语句</li>
        
        
    
        <li class="chapter " data-level="6.1" >
            
                <span>
            
                    
                    step7：作用域和块语句
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1.1" data-path="../lab7/intro.html">
            
                <a href="../lab7/intro.html">
            
                    
                    任务概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.2" data-path="../lab7/guide.html">
            
                <a href="../lab7/guide.html">
            
                    
                    实验指导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.3" data-path="../lab7/spec.html">
            
                <a href="../lab7/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6.2" >
            
                <span>
            
                    
                    step8：循环语句
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.2.1" data-path="../lab8/intro.html">
            
                <a href="../lab8/intro.html">
            
                    
                    任务概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.2" data-path="../lab8/guide.html">
            
                <a href="../lab8/guide.html">
            
                    
                    实验指导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.3" data-path="../lab8/spec.html">
            
                <a href="../lab8/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">函数和全局变量</li>
        
        
    
        <li class="chapter " data-level="7.1" >
            
                <span>
            
                    
                    step9：函数
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1.1" data-path="part0-intro.html">
            
                <a href="part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.2" data-path="part1-parser.html">
            
                <a href="part1-parser.html">
            
                    
                    词法语法分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.3" data-path="part1-1-task.html">
            
                <a href="part1-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.4" data-path="part4-1-cconv.html">
            
                <a href="part4-1-cconv.html">
            
                    
                    调用约定
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="7.1.5" data-path="part4-2-codegen.html">
            
                <a href="part4-2-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.6" data-path="part4-3-task.html">
            
                <a href="part4-3-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.7" data-path="summary.html">
            
                <a href="summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.8" data-path="spec.html">
            
                <a href="spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7.2" >
            
                <span>
            
                    
                    step10：全局变量
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.2.1" data-path="../lab10/part0-intro.html">
            
                <a href="../lab10/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.2" data-path="../lab10/part1-parser.html">
            
                <a href="../lab10/part1-parser.html">
            
                    
                    词法语法分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.3" data-path="../lab10/part1-1-task.html">
            
                <a href="../lab10/part1-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.4" data-path="../lab10/part4-codegen.html">
            
                <a href="../lab10/part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.5" data-path="../lab10/part4-1-task.html">
            
                <a href="../lab10/part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.6" data-path="../lab10/summary.html">
            
                <a href="../lab10/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.7" data-path="../lab10/spec.html">
            
                <a href="../lab10/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">指针和数组</li>
        
        
    
        <li class="chapter " data-level="8.1" >
            
                <span>
            
                    
                    step11：指针
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1.1" data-path="../lab11/part0-intro.html">
            
                <a href="../lab11/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.2" data-path="../lab11/typeck.html">
            
                <a href="../lab11/typeck.html">
            
                    
                    类型检查
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.3" data-path="../lab11/part4-codegen.html">
            
                <a href="../lab11/part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.4" data-path="../lab11/part4-1-task.html">
            
                <a href="../lab11/part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.5" data-path="../lab11/summary.html">
            
                <a href="../lab11/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.6" data-path="../lab11/spec.html">
            
                <a href="../lab11/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8.2" >
            
                <span>
            
                    
                    step12：数组
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.2.1" data-path="../lab12/part0-intro.html">
            
                <a href="../lab12/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.2.2" data-path="../lab12/part4-codegen.html">
            
                <a href="../lab12/part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.2.3" data-path="../lab12/part4-1-task.html">
            
                <a href="../lab12/part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.2.4" data-path="../lab12/summary.html">
            
                <a href="../lab12/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.2.5" data-path="../lab12/spec.html">
            
                <a href="../lab12/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">参考资料</li>
        
        
    
        <li class="chapter " data-level="9.1" data-path="../../REFERENCE.html">
            
                <a href="../../REFERENCE.html">
            
                    
                    参考资料
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >代码生成</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x4EE3;&#x7801;&#x751F;&#x6210;">&#x4EE3;&#x7801;&#x751F;&#x6210;</h2>
<p>&#x9996;&#x5148;&#x786E;&#x5B9A;&#x4F60;&#x8981;&#x9075;&#x5FAA;&#x7684;&#x8C03;&#x7528;&#x7EA6;&#x5B9A;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E0A;&#x4E00;&#x8282;&#x4E2D;&#x6211;&#x4EEC;&#x7ED9;&#x51FA;&#x7684;&#x7B80;&#x5316;&#x7248;&#x8C03;&#x7528;&#x7EA6;&#x5B9A;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x53C2;&#x8003; GCC &#x7684;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x81EA;&#x5DF1;&#x518D;&#x5B9A;&#x4E49;&#x4E00;&#x5957;&#x3002;&#x4E0D;&#x8FC7;&#xFF0C;&#x4F60;&#x7684;&#x7F16;&#x8BD1;&#x5668;&#x5E94;&#x8BE5;&#x59CB;&#x7EC8;&#x9075;&#x5FAA;&#x540C;&#x4E00;&#x5957;&#x8C03;&#x7528;&#x7EA6;&#x5B9A;&#x3002;&#x672C;&#x5C0F;&#x8282;&#x4E4B;&#x540E;&#x7684;&#x63CF;&#x8FF0;&#x90FD;&#x57FA;&#x4E8E;&#x4E0A;&#x4E00;&#x8282;&#x7684;&#x7B80;&#x5316;&#x7248;&#x8C03;&#x7528;&#x7EA6;&#x5B9A;&#x3002;</p>
<h3 id="&#x51FD;&#x6570;&#x5E8F;&#x8A00;">&#x51FD;&#x6570;&#x5E8F;&#x8A00;</h3>
<p>&#x5728; lab4 &#x6211;&#x4EEC;&#x7B80;&#x5355;&#x63D0;&#x53CA;&#x4E86;&#x51FD;&#x6570;&#x7684;<strong>&#x5E8F;&#x8A00;</strong>&#xFF08;prologue&#xFF09;&#x4E0E;<strong>&#x6536;&#x5C3E;</strong>&#xFF08;epilogue&#xFF09;&#xFF0C;&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x5C06;&#x5BF9;&#x8FD9;&#x4E24;&#x90E8;&#x5206;&#x4EE3;&#x7801;&#x7684;&#x751F;&#x6210;&#x8FDB;&#x884C;&#x5B8C;&#x5584;&#x3002;</p>
<p>&#x5F53;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x5F00;&#x59CB;&#x6267;&#x884C;&#x524D;&#xFF0C;&#x9700;&#x8981;&#x505A;&#x4E00;&#x4E9B;&#x989D;&#x5916;&#x5DE5;&#x4F5C;&#xFF0C;&#x5373;&#x51FD;&#x6570;&#x5E8F;&#x8A00;&#xFF0C;&#x8FD9;&#x4E00;&#x90E8;&#x5206;&#x7684;&#x5DE5;&#x4F5C;&#x5305;&#x62EC;&#xFF1A;</p>
<ul>
<li>&#x521B;&#x5EFA;&#x6808;&#x5E27;&#xFF1A;&#x6839;&#x636E;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x548C;&#x9700;&#x8981;&#x4FDD;&#x5B58;&#x7684;&#x5BC4;&#x5B58;&#x5668;&#x7684;&#x4E2A;&#x6570;&#xFF0C;&#x6808;&#x5E27;&#x7684;&#x5927;&#x5C0F;&#x662F;&#x53EF;&#x4EE5;&#x4E8B;&#x5148;&#x786E;&#x5B9A;&#x7684;&#xFF0C;&#x76F4;&#x63A5;&#x4EE4; <code>sp</code> &#x51CF;&#x53BB;&#x6808;&#x5E27;&#x5927;&#x5C0F;&#x3002;&#x5728;&#x67D0;&#x4E9B;&#x7CFB;&#x7EDF;&#x4E0B;&#xFF0C;&#x9700;&#x8981;&#x8BA9;&#x6808;&#x6307;&#x9488; 16 &#x5B57;&#x8282;&#x5BF9;&#x9F50;&#xFF0C;&#x4E0D;&#x8FC7;&#x672C;&#x5B9E;&#x9A8C;&#x4E2D;&#x6211;&#x4EEC;&#x5C06;&#x5176;&#x7701;&#x7565;&#x3002;</li>
<li>&#x4FDD;&#x5B58;&#x5BC4;&#x5B58;&#x5668;&#xFF1A;&#x5728;&#x8FD9;&#x4E00;&#x90E8;&#x5206;&#x9700;&#x8981;&#x4FDD;&#x5B58;&#x7684;&#x5BC4;&#x5B58;&#x5668;&#x5373;<strong>&#x88AB;&#x8C03;&#x7528;&#x8005;&#x4FDD;&#x5B58;</strong>&#x5BC4;&#x5B58;&#x5668;&#xFF0C;&#x5728; RISC-V &#x4E2D;&#x5373;&#x4EE5; <code>s</code> &#x5F00;&#x5934;&#x7684;&#x90A3;&#x4E9B;&#x5BC4;&#x5B58;&#x5668;&#xFF0C;&#x4E0D;&#x8FC7;&#x5728;&#x672C;&#x5B9E;&#x9A8C;&#x4E2D;&#x6211;&#x4EEC;&#x53EA;&#x7528;&#x5230;&#x4E86; <code>s0</code>&#xFF08;&#x5373; <code>fp</code>&#xFF09;&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x9700;&#x8981;&#x4FDD;&#x5B58;&#x5176;&#x4ED6;&#x7684;&#x3002;&#x7279;&#x522B;&#x5730;&#xFF0C;&#x5982;&#x679C;&#x5728;&#x8BE5;&#x51FD;&#x6570;&#x4E2D;&#x8C03;&#x7528;&#x4E86;&#x53E6;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x8FD4;&#x56DE;&#x5730;&#x5740;&#x5BC4;&#x5B58;&#x5668; <code>ra</code> &#x4E5F;&#x4F1A;&#x88AB;&#x4FEE;&#x6539;&#xFF0C;&#x6211;&#x4EEC;&#x4E5F;&#x5C06;&#x5B83;&#x4FDD;&#x5B58;&#x5728;&#x8FD9;&#x91CC;&#xFF0C;&#x4F46;&#x5B83;&#x5176;&#x5B9E;&#x662F;&#x5C5E;&#x4E8E;<strong>&#x8C03;&#x7528;&#x8005;&#x4FDD;&#x5B58;</strong>&#x5BC4;&#x5B58;&#x5668;&#x3002;</li>
<li>&#x8BBE;&#x7F6E;&#x6808;&#x5E27;&#x6307;&#x9488;&#xFF1A;&#x51FD;&#x6570;&#x4E2D;&#x7528;&#x5230;&#x7684;&#x6240;&#x6709;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x90FD;&#x662F;&#x4FDD;&#x5B58;&#x7684;&#x6808;&#x5E27;&#x4E2D;&#x7684;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x201C;&#x6808;&#x5E27;&#x57FA;&#x5740;&#x5BC4;&#x5B58;&#x5668;&#x201D;&#x52A0;&#x201C;&#x504F;&#x79FB;&#x201D;&#x7684;&#x5F62;&#x5F0F;&#x6765;&#x5F15;&#x7528;&#x5B83;&#x4EEC;&#x3002;&#x4E0D;&#x8FC7;&#x6211;&#x4EEC;&#x4E0D;&#x80FD;&#x76F4;&#x63A5;&#x4F7F;&#x7528; <code>sp</code> &#x4F5C;&#x4E3A;&#x6808;&#x5E27;&#x57FA;&#x5740;&#x5BC4;&#x5B58;&#x5668;&#xFF0C;&#x56E0;&#x4E3A;&#x5728;&#x540E;&#x7EED;&#x6808;&#x5F0F;&#x673A;&#x7684;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x4E2D;&#x6808;&#x53EF;&#x80FD;&#x4F1A;&#x589E;&#x957F;&#x3002;&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x7528;&#x4E00;&#x4E2A;&#x7279;&#x6B8A;&#x5BC4;&#x5B58;&#x5668; <code>fp</code>&#xFF08;&#x7B49;&#x4EF7;&#x4E8E; <code>s0</code>&#xFF09;&#x6765;&#x4FDD;&#x5B58;&#x6808;&#x5E27;&#x57FA;&#x5740;&#xFF0C;&#x5373;<strong>&#x6808;&#x5E27;&#x6307;&#x9488;</strong>&#xFF08;frame pointer&#xFF09;&#xFF0C;&#x5E76;&#x5C06;&#x5176;&#x8BBE;&#x4E3A;&#x539F;&#x6765;&#x7684; <code>sp</code>&#x3002;</li>
</ul>
<p>&#x751F;&#x6210;&#x7684;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-asm">.globl func:                    # &#x8BBE;&#x7F6E;&#x6807;&#x7B7E; func &#x4E3A;&#x5168;&#x5C40;&#x53EF;&#x89C1;
func:                           # &#x51FD;&#x6570;&#x5165;&#x53E3;&#x6807;&#x7B7E;
    addi sp, sp, -framesize     # &#x521B;&#x5EFA;&#x6808;&#x5E27;
    sw ra, framesize-4(sp)      # &#x4FDD;&#x5B58;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x5730;&#x5740;
    sw fp, framesize-8(sp)      # &#x4FDD;&#x5B58;&#x4E0A;&#x4E00;&#x51FD;&#x6570;&#x6808;&#x5E27;&#x6307;&#x9488;
    addi fp, sp, framesize      # &#x8BBE;&#x7F6E;&#x65B0;&#x7684;&#x6808;&#x5E27;&#x6307;&#x9488;
</code></pre>
<h3 id="&#x51FD;&#x6570;&#x6536;&#x5C3E;">&#x51FD;&#x6570;&#x6536;&#x5C3E;</h3>
<p>&#x5F53;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x8FD0;&#x884C;&#x7ED3;&#x675F;&#xFF0C;&#x6267;&#x884C; <code>return</code> &#x8BED;&#x53E5;&#x540E;&#xFF0C;&#x8FD8;&#x4E0D;&#x80FD;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#xFF0C;&#x8FD8;&#x9700;&#x8981;&#x4E00;&#x4E9B;&#x6536;&#x5C3E;&#x5DE5;&#x4F5C;&#xFF0C;&#x5373;&#x51FD;&#x6570;&#x6536;&#x5C3E;&#x3002;&#x8FD9;&#x4E00;&#x90E8;&#x5206;&#x7684;&#x5DE5;&#x4F5C;&#x5305;&#x62EC;&#xFF1A;</p>
<ul>
<li>&#x6062;&#x590D;&#x5BC4;&#x5B58;&#x5668;&#xFF1A;&#x6062;&#x590D;&#x90A3;&#x4E9B;&#x5728;&#x5E8F;&#x8A00;&#x90E8;&#x5206;&#x4FDD;&#x5B58;&#x7684;&#x5BC4;&#x5B58;&#x5668;&#xFF0C;&#x5373;&#x8FD4;&#x56DE;&#x5730;&#x5740; <code>ra</code> &#x548C;&#x6808;&#x5E27;&#x6307;&#x9488; <code>fp</code>&#x3002;</li>
<li>&#x9500;&#x6BC1;&#x6808;&#x5E27;&#xFF1A;&#x4EE4; <code>sp</code> &#x52A0;&#x4E0A;&#x6808;&#x5E27;&#x5927;&#x5C0F;&#xFF0C;&#x4FDD;&#x8BC1; <code>sp</code> &#x5728;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x524D;&#x540E;&#x4E0D;&#x53D8;&#x3002;</li>
<li>&#x8BBE;&#x7F6E;&#x8FD4;&#x56DE;&#x503C;&#x5E76;&#x8FD4;&#x56DE;&#xFF1A;&#x5C06; <code>return</code> &#x8BED;&#x53E5;&#x4E2D;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x4FDD;&#x5B58;&#x5230; <code>a0</code> &#x5BC4;&#x5B58;&#x5668;&#xFF0C;&#x7136;&#x540E;&#x6267;&#x884C; <code>ret</code> &#x6307;&#x4EE4;&#x3002;</li>
</ul>
<p>&#x751F;&#x6210;&#x7684;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-asm">func_epilogue:                  # &#x51FD;&#x6570;&#x6536;&#x5C3E;&#x90E8;&#x5206;&#x6807;&#x7B7E;
    lw ra, framesize-4(sp)      # &#x6062;&#x590D;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x5730;&#x5740;&#x5230; ra
    lw fp, framesize-8(sp)      # &#x6062;&#x590D;&#x4E0A;&#x4E00;&#x51FD;&#x6570;&#x6808;&#x5E27;&#x6307;&#x9488;&#x5230; fp
    addi sp, sp, framesize      # &#x9500;&#x6BC1;&#x6808;&#x5E27;
    mv a0, retval               # &#x5C06;&#x8FD4;&#x56DE;&#x503C;&#x4FDD;&#x5B58;&#x5230; a0 &#x5BC4;&#x5B58;&#x5668;
    ret                         # &#x7B49;&#x4EF7;&#x4E8E; jr ra
</code></pre>
<p>&#x6B64;&#x5916;&#xFF0C;&#x5728;&#x4E4B;&#x524D;&#x7684; lab4 &#x4E5F;&#x63D0;&#x5230;&#x8FC7;&#xFF0C;&#x5BF9;&#x4E8E;&#x6240;&#x6709;&#x7684; <code>return</code> &#x8BED;&#x53E5;&#xFF0C;&#x5E94;&#x8BE5;&#x751F;&#x6210;&#x4E00;&#x6761;&#x76F4;&#x63A5;&#x8C03;&#x8F6C;&#x5230;&#x51FD;&#x6570;&#x6536;&#x5C3E;&#x7684;&#x8BED;&#x53E5;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x76F4;&#x63A5;&#x4E00;&#x6761; <code>ret</code> &#x6307;&#x4EE4;&#x3002;</p>
<h3 id="&#x51FD;&#x6570;&#x8C03;&#x7528;">&#x51FD;&#x6570;&#x8C03;&#x7528;</h3>
<p>&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x7528;&#x5230;&#x4E00;&#x6761;&#x65B0;&#x6307;&#x4EE4; <code>call label</code>&#xFF0C;&#x5176;&#x539F;&#x578B;&#x4E3A; <code>jal ra, label</code>&#xFF0C;&#x8868;&#x793A;&#x201C;&#x8DF3;&#x8F6C;&#x5230; <code>label</code>&#xFF0C;&#x5E76;&#x628A;&#x8FD4;&#x56DE;&#x5730;&#x5740;&#x4FDD;&#x5B58;&#x5230; <code>ra</code>&#x201D;&#xFF0C;&#x800C; <code>call</code> &#x53EA;&#x662F;&#x4E00;&#x6761;&#x4F2A;&#x6307;&#x4EE4;&#x3002;</p>
<p>&#x5F53;&#x8981;&#x8C03;&#x7528;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x65F6;&#xFF0C;&#x4E0D;&#x5149;&#x662F;&#x4E00;&#x6761; <code>call</code> &#x6307;&#x4EE4;&#x5C31;&#x53EF;&#x4EE5;&#x89E3;&#x51B3;&#x7684;&#xFF0C;&#x8FD8;&#x9700;&#x8981;&#x8003;&#x8651;&#x4EE5;&#x4E0B;&#x65B9;&#x9762;&#xFF1A;</p>
<ul>
<li>&#x53C2;&#x6570;&#x4F20;&#x9012;&#xFF1A;&#x6839;&#x636E;&#x6211;&#x4EEC;&#x7684;&#x8C03;&#x7528;&#x7EA6;&#x5B9A;&#xFF0C;&#x4F7F;&#x7528;&#x6808;&#x4ECE;&#x53F3;&#x5F80;&#x5DE6;&#x8FDB;&#x884C;&#x4F20;&#x53C2;&#x3002;</li>
<li>&#x5BC4;&#x5B58;&#x5668;&#x7684;&#x4FDD;&#x5B58;&#x4E0E;&#x6062;&#x590D;&#xFF1A;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5728;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x524D;&#x4FDD;&#x5B58;<strong>&#x8C03;&#x7528;&#x8005;&#x4FDD;&#x5B58;</strong>&#x5BC4;&#x5B58;&#x5668;&#xFF0C;&#x5E76;&#x5728;&#x8C03;&#x7528;&#x7ED3;&#x675F;&#x540E;&#x6062;&#x590D;&#x3002;&#x4E0D;&#x8FC7;&#x672C;&#x5B9E;&#x9A8C;&#x4F7F;&#x7528;&#x6808;&#x5F0F;&#x673A;&#x6A21;&#x578B;&#xFF0C;&#x6CA1;&#x6709;&#x5BC4;&#x5B58;&#x5668;&#x7684;&#x5206;&#x914D;&#xFF0C;&#x5728;&#x6808;&#x5F0F;&#x673A;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x4E2D;&#x5DF2;&#x7ECF;&#x8FDB;&#x884C;&#x4E86;&#x5BC4;&#x5B58;&#x5668;&#x7684;&#x4FDD;&#x5B58;&#xFF0C;&#x56E0;&#x6B64;&#x65E0;&#x9700;&#x4E13;&#x95E8;&#x4FDD;&#x5B58;&#x8C03;&#x7528;&#x8005;&#x5BC4;&#x5B58;&#x5668;&#x3002;</li>
</ul>
<p>&#x4E8E;&#x662F;&#x5BF9;&#x4E8E;&#x51FD;&#x6570;&#x8C03;&#x7528;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x751F;&#x6210;&#x4EE5;&#x4E0B;&#x6C47;&#x7F16;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-asm">addi sp, sp, -arg_num*4     # &#x5728;&#x6808;&#x4E0A;&#x5206;&#x914D;&#x4E00;&#x5757;&#x533A;&#x57DF;&#x7528;&#x4E8E;&#x5B58;&#x653E;&#x53C2;&#x6570;
sw arg_n-1, n*4-4(sp)       # &#x4ECE;&#x53F3;&#x5411;&#x5DE6;&#x4F9D;&#x6B21;&#x5411;&#x6808;&#x4E2D;&#x538B;&#x5165;&#x53C2;&#x6570;
...
sw arg_1, 4(sp)
sw arg_0, 0(sp)
call func                   # &#x8C03;&#x7528;&#x51FD;&#x6570; func&#xFF0C;&#x786C;&#x4EF6;&#x4F1A;&#x81EA;&#x52A8;&#x8BBE;&#x7F6E; ra &#x4E3A;&#x4E0B;&#x4E00;&#x6761;&#x6307;&#x4EE4;&#x7684;&#x5730;&#x5740;
addi sp, sp, arg_num*4      # &#x56DE;&#x6536;&#x6808;&#x7A7A;&#x95F4;

addi sp, sp, -4
sw a0, 0(sp)                # &#x5C06;&#x8FD4;&#x56DE;&#x503C;&#x538B;&#x5165;&#x6808;&#xFF0C;&#x7EE7;&#x7EED;&#x6808;&#x5F0F;&#x673A;&#x7684;&#x6267;&#x884C;
</code></pre>
<p>&#x9700;&#x8981;&#x6CE8;&#x610F;&#xFF0C;&#x5728;&#x51FD;&#x6570; <code>func</code> &#x4E2D;&#x5F15;&#x7528;&#x53C2;&#x6570;&#x65F6;&#xFF0C;&#x8981;&#x80FD;&#x786E;&#x5B9A;&#x53C2;&#x6570;&#x7684;&#x4F4D;&#x7F6E;&#xFF0C;&#x5373;&#x7B97;&#x51FA;&#x6B63;&#x786E;&#x7684;&#x76F8;&#x5BF9;&#x4E8E; <code>fp</code> &#x7684;&#x504F;&#x79FB;&#x3002;</p>
<h3 id="&#x6808;&#x7A7A;&#x95F4;&#x5E03;&#x5C40;">&#x6808;&#x7A7A;&#x95F4;&#x5E03;&#x5C40;</h3>
<p>&#x4E3A;&#x4E86;&#x65B9;&#x4FBF;&#x7406;&#x89E3;&#xFF0C;&#x8FD9;&#x91CC;&#x7ED9;&#x51FA;&#x4E86;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x65F6;&#x7684;&#x6808;&#x7A7A;&#x95F4;&#x5E03;&#x5C40;&#xFF0C;&#x4EE5;&#x4F9B;&#x53C2;&#x8003;&#xFF1A;</p>
<p><img src="pics/stack_frame.svg" alt="&#x6808;&#x7A7A;&#x95F4;&#x5E03;&#x5C40;"></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="part4-1-cconv.html" class="navigation navigation-prev " aria-label="Previous page: 调用约定">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="part4-3-task.html" class="navigation navigation-next " aria-label="Next page: 任务">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"代码生成","level":"7.1.5","depth":2,"next":{"title":"任务","level":"7.1.6","depth":2,"path":"docs/lab9/part4-3-task.md","ref":"docs/lab9/part4-3-task.md","articles":[]},"previous":{"title":"调用约定","level":"7.1.4","depth":2,"path":"docs/lab9/part4-1-cconv.md","ref":"docs/lab9/part4-1-cconv.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["hide-element","chapter-fold","katex","alerts","emphasize","mermaid-gb3","codeblock-label","code","search-pro","click-reveal","expandable-chapters-interactive","localized-footer"],"pluginsConfig":{"chapter-fold":{},"emphasize":{},"codeblock-label":{},"search-pro":{},"search":{},"localized-footer":{"filename":"gitalk.html","hline":"true"},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"code":{"copyButtons":true},"hide-element":{},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":1},"click-reveal":{},"highlight":{},"mermaid-gb3":{},"expandable-chapters-interactive":{},"alerts":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"docs/lab9/part4-2-codegen.md","mtime":"2020-09-02T07:27:37.427Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-09-07T12:13:30.006Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-click-reveal/click_reveal.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    <script src="../../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>

    </body>
</html>

